<?php
class ControllerReportV2Buyer extends Controller {

    public function index() {
        $this->load->model('reportV2/buyer');
        $data = $this->_initData();
        $pobuyer = $this->model_reportV2_buyer->getPobuyer();
        $inventqty = $this->model_reportV2_buyer->getInvenTotal($pobuyer);
        $pclass = $this->model_reportV2_buyer->getbuyer($inventqty);
        $data['pclass'] = $pclass;
        // var_dump($data['pclass']);
        $this->response->setOutput($this->load->view('reportV2/buyer.tpl', $data));
    }

    protected function _initData($data=array()) {
        $data['token'] = $this->session->data['token'];
        $data['breadcrumbs'] = array(
            array(
                'text' => '首页',
                'href' => $this->url->link('common/dashboard', 'token='.$data['token'], 'SSL'),
            ),
            array(
                'text' => '采销员业绩统计',
                'href' => $this->url->link('reportV2/buyer', 'token='.$data['token'], 'SSL'),
            ),
        );
        
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $data['export'] = $this->url->link('reportV2/buyer/exportLists', 'token=' . $this->session->data['token'] . $url, 'SSL');
        return $data;
    }

    public function exportLists(){
        $this->load->model('reportV2/buyer');
        $inventqty = $this->model_reportV2_buyer->getInvenTotal();
        $pclass = $this->model_reportV2_buyer->getbuyer($inventqty);
            $i=0;
           
        foreach ($pclass as $key => $value) {
            $totalTPLDy=0;
            $totalTPLMNy=0;
            $totalTCLMNy=0;
            $totalTPTYy=0;
            $totalTCTYy=0;
            $totaltotalpricey=0;
            foreach ($value as $yl => $ylv) {
                $i++;
                $save_data[$i] = array(
                'GLNAME'          =>  '',
                'name'   =>  $ylv['name'],
                'TPLD'           =>  $ylv['TPLD'],
                'TPLMN'         =>  $ylv['TPLMN'],
                'GPRLMN'    =>  $ylv['GPRLMN'],
                'TPTY' =>  $ylv['TPTY'],
                'GPRTY'    =>  $ylv['GPRTY'],
                'totalprice'            =>  $ylv['totalprice'],
                'SSC'    =>  $ylv['SSC'],
                );
                if ($key=='YL') {
                    $save_data[$i]['GLNAME'] = '应亮';
                }elseif($key=='DM'){
                    $save_data[$i]['GLNAME'] = '董敏';
                }elseif($key=='ZDM'){
                    $save_data[$i]['GLNAME'] = '周东明';
                }elseif($key=='QT'){
                    $save_data[$i]['GLNAME'] = '其他';
                }
                $totalTPLDy+= (float)$ylv['TPLD'];
                $totalTPLMNy+= $ylv['TPLMN'];
                $totalTCLMNy+= $ylv['TCLMN'];
                $totalTPTYy+= $ylv['TPTY'];
                $totalTCTYy+= $ylv['TCTY'];
                $totaltotalpricey+= $ylv['totalprice'];
                
            }
            $i++;
            $save_data[$i] = array(
                'GLNAME'          => '合计',
                'name'   =>  '--',
                'TPLD'           =>  $totalTPLDy,
                'TPLMN'         =>  $totalTPLMNy,
                'GPRLMN'    =>  (round(($totalTPLMNy-$totalTCLMNy)/$totalTPLMNy, 4)*100).'%',
                'TPTY' =>  $totalTPTYy,
                'GPRTY'    =>  (round(($totalTPTYy-$totalTCTYy)/$totalTPTYy, 4)*100).'%',
                'totalprice'            =>  $totaltotalpricey,
                'SSC'    =>  '--',
            );
            $totalTPLD+=$totalTPLDy ;
            $totalTPLMN+= $totalTPLMNy;
            $totalTCLMN+= $totalTCLMNy;
            $totalTPTY+= $totalTPTYy;
            $totalTCTY+= $totalTCTYy;
            $totaltotalprice+= $totaltotalpricey;
        }
        $save_data[$i+1] = array(
                'GLNAME'          => '百货栈合计',
                'name'   =>  '--',
                'TPLD'           =>  $totalTPLD,
                'TPLMN'         =>  $totalTPLMN,
                'GPRLMN'    =>  (round(($totalTPLMN-$totalTCLMN)/$totalTPLMN, 4)*100).'%',
                'TPTY' =>  $totalTPTY,
                'GPRTY'    =>  (round(($totalTPTY-$totalTCTY)/$totalTPTY, 4)*100).'%',
                'totalprice'            =>  $totaltotalprice,
                'SSC'    =>  '--',
            );
    $this->load->library('PHPExcel/PHPExcel');
    $objPHPExcel = new PHPExcel();    
    $objProps = $objPHPExcel->getProperties();    
    $objProps->setCreator("Think-tec");
    $objProps->setLastModifiedBy("Think-tec");    
    $objProps->setTitle("Think-tec Contact");    
    $objProps->setSubject("Think-tec Contact Data");    
    $objProps->setDescription("Think-tec Contact Data");    
    $objProps->setKeywords("Think-tec Contact");    
    $objProps->setCategory("Think-tec");
    $objPHPExcel->setActiveSheetIndex(0);     
    $objActSheet = $objPHPExcel->getActiveSheet(); 
       
    $objActSheet->setTitle('Sheet1');
    $col_idx = 'A';
    $headers = array( '采销员','采销员','昨天销量额 ', '月至今销量额 ','月至今毛利','年至今销量额','年至今毛利','库存金额', '库存天数');
    $row_keys = array('GLNAME','name','TPLD','TPLMN','GPRLMN','TPTY','GPRTY','totalprice',   'SSC');
    foreach ($headers as $header) {
      $objActSheet->setCellValue($col_idx++.'1', $header);  
    }
    //添加物流信息
    $i = 2;
    foreach ($save_data as $rlst) {
      $col_idx = 'A';
      foreach ($row_keys as $rk) {
        // $objActSheet->setCellValue($col_idx++.$i, $rlst[$rk]); 
        $objActSheet->setCellValueExplicit($col_idx++.$i, $rlst[$rk], PHPExcel_Cell_DataType::TYPE_STRING);
      }
      $i++;
    } 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    
    ob_end_clean();
    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="main_purchase_order_'.date('Y-m-d',time()).'.xlsx"');
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

    // If you're serving to IE over SSL, then the following may be needed
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0
    $objWriter->save('php://output'); 
    exit;
    }

}
